package com.test;

import com.google.gson.Gson;
import com.iab.gdpr.Purpose;
import com.iab.gdpr.consent.VendorConsent;
import com.iab.gdpr.consent.VendorConsentEncoder;
import com.iab.gdpr.consent.implementation.v1.VendorConsentBuilder;
import com.iab.gdpr.consent.range.RangeEntry;
import com.iab.gdpr.consent.range.SingleRangeEntry;
import com.test.beans.GDPRCountryList;
import com.test.beans.VendorBean;

import java.io.File;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okio.BufferedSource;
import okio.Okio;

public class GDPRConsentGenerator {
    final Set<Purpose> allowedPurposes = new HashSet<>(Arrays.asList(Purpose.STORAGE_AND_ACCESS, Purpose.PERSONALIZATION, Purpose.AD_SELECTION, Purpose.CONTENT_DELIVERY, Purpose.MEASUREMENT));
    final String consentLanguage = "EN";
    private int vendorListVersion = 206;
    int maxVendorId = 0;
    final int vendorEncodingType = 1;
    static final Set<Integer> allowedVendors = new HashSet<>(Arrays.asList(761, 759, 736, 734, 688, 667, 667, 657, 609, 606, 587, 559, 544, 523, 512, 506, 494, 484, 468, 467, 466, 452, 450, 447, 443, 431, 423, 416, 415, 409, 397, 394, 388, 385, 384, 381, 373, 349, 345, 338, 333, 328, 317, 315, 312, 309, 308, 304, 302, 299, 295, 294, 284, 281, 278, 275, 273, 264, 262, 246, 245, 243, 241, 226, 214, 213, 212, 209, 208, 200, 195, 192, 183, 183, 173, 173, 168, 165, 163, 161, 156, 152, 151, 147, 144, 140, 139, 137, 132, 130, 129, 126, 124, 119, 110, 109, 98, 97, 95, 93, 91, 88, 86, 85, 84, 82, 82, 81, 81, 79, 78, 77, 76, 76, 73, 72, 71, 69, 69, 68, 67, 67, 63, 60, 59, 57, 52, 52, 50, 45, 45, 42, 37, 36, 34, 32, 32, 31, 28, 28, 27, 25, 25, 24, 24, 23, 22, 21, 18, 16, 15, 13, 12, 11, 10, 9, 4, 3, 1));


    public static void main(String[] args) {
        System.out.println(TimeUnit.DAYS.toMillis(90));
        System.out.println(90*24*60*60*1000l);
        GDPRConsentGenerator gdprTest = new GDPRConsentGenerator();
        try {
/*            BufferedSource bufferedSource = Okio.buffer(Okio.source(new File("D:\\vendorlist.json")));

            VendorBean bean = new Gson().fromJson(bufferedSource.readByteString().utf8(), VendorBean.class);
            gdprTest.vendorListVersion = bean.vendorListVersion;

            System.out.println(bean.vendorListVersion);
            System.out.println(bean.vendors.get(0));*/

            BufferedSource bufferedSource = Okio.buffer(Okio.source(new File("D:\\develop\\workspace\\trunk\\app\\src\\main\\assets\\GDPR_Partner.json")));
            String content = bufferedSource.readByteString().utf8();
            GDPRCountryList list = new Gson().fromJson(content, GDPRCountryList.class);
            final Set<Integer> allowedPurposes = new HashSet<>(list.sdks.size());
            System.out.println("json has " + list.sdks.size());
            for (GDPRCountryList.SdksBean sdk : list.sdks) {
          /*      final int vendorIdByName = getVendorIdByName(sdk.getName(), bean.vendors);*/
                if (sdk.IABID == 0) {
                    continue;
                }
                allowedPurposes.add(sdk.IABID);
                if (sdk.IABID > gdprTest.maxVendorId) {
                    gdprTest.maxVendorId = sdk.IABID;
                }
            }
            System.out.println("maxVendorId=" + gdprTest.maxVendorId);

            System.out.println("sdk size=" + (allowedPurposes.size()));

            System.out.println(new Gson().toJson(allowedPurposes));

            System.out.println(allowedPurposes.size() + " " + allowedVendors.size());
            System.out.println(gdprTest.vendorConsentEncoder(allowedPurposes));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static int getVendorIdByName(String name, List<VendorBean.VendorListBean> vendorListBeanList) {
        for (VendorBean.VendorListBean bean : vendorListBeanList) {
            if (bean.name.contains(name)) return bean.id;
        }
        return -1;
    }


    private String vendorConsentEncoder(Set<Integer> allowedVendors) {
        final List<RangeEntry> rangeEntries = new ArrayList<>();
        for (Integer vendorId : allowedVendors) {
            rangeEntries.add(new SingleRangeEntry(vendorId));
        }

        final VendorConsent vendorConsent = new VendorConsentBuilder()
                .withConsentRecordCreatedOn(Instant.now())
                .withConsentRecordLastUpdatedOn(Instant.now())
                .withConsentLanguage(consentLanguage)
                .withVendorListVersion(vendorListVersion)
                .withAllowedPurposes(allowedPurposes)
                .withMaxVendorId(maxVendorId)
                .withVendorEncodingType(vendorEncodingType)
                .withDefaultConsent(false)
                .withRangeEntries(rangeEntries)
                .withBitField(allowedVendors)
                .build();
        final String base64String = VendorConsentEncoder.toBase64String(vendorConsent);
        return base64String;
    }

}
